package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.mapper.PaymentMapper;
import com.woniuxy.model.entity.Payment;
import com.woniuxy.model.entity.TransactionOrder;
import com.woniuxy.service.TransactionOrderService;
import com.woniuxy.mapper.TransactionOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【transaction_order】的数据库操作Service实现
* @createDate 2025-01-06 10:10:10
*/
@Slf4j
@Service
public class TransactionOrderServiceImpl implements TransactionOrderService {

    @Autowired
    private TransactionOrderMapper transactionOrderMapper;


    @Autowired
    private PaymentMapper paymentMethodMapper;

    /**
     * 支付订单
     */
    @Override
    public boolean payOrder(String orderNo, Long paymentMethodId) {
        // 1. 查询订单信息
        TransactionOrder order = transactionOrderMapper.selectOne(new QueryWrapper<TransactionOrder>().eq("order_no", orderNo));
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }

        // 2. 校验订单状态
        if (order.getStatus() != 1) { // 1: 充电已完成
            throw new RuntimeException("订单未完成，无法支付");
        }

        // 3. 校验是否已支付
        if (order.getPaymentStatus() == 1) { // 1: 已付款
            throw new RuntimeException("订单已支付");
        }

        // 4. 校验支付方式是否有效
        Payment paymentMethod = paymentMethodMapper.selectById(paymentMethodId);
        if (paymentMethod == null) {
            throw new RuntimeException("无效的支付方式");
        }

        // 5. 模拟支付逻辑
        boolean paymentSuccess = processPayment(order, paymentMethod);
        if (!paymentSuccess) {
            throw new RuntimeException("支付失败");
        }

        // 6. 更新订单支付状态
        order.setPaymentStatus(1); // 1: 已付款
        order.setUpdateTime(new Date());
        order.setPaymentMethodId(paymentMethodId); // 更新支付方式
        transactionOrderMapper.updateById(order);

        return true;
    }

    private boolean processPayment(TransactionOrder order, Payment paymentMethod) {
        // 模拟支付逻辑，这里可以调用第三方支付网关
        log.info("模拟支付中... 订单号: {}, 金额: {}, 支付方式: {}", order.getOrderNo(), order.getTransactionAmount(), paymentMethod.getPaymentMethodName());
        return true; // 返回 true 表示支付成功
    }



    @Override
    public TransactionOrder getTransactionByOrderNo(String orderNo) {
        return transactionOrderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public void createTransactionOrder(TransactionOrder transactionOrder) {
        transactionOrder.setCreateTime(new Date());
        transactionOrder.setUpdateTime(new Date());
        transactionOrder.setPaymentStatus(0); // 默认待付款
        transactionOrderMapper.insert(transactionOrder);
    }

    @Override
    public void updateTransactionStatus(String orderNo, Integer status, Integer paymentStatus) {
        TransactionOrder transactionOrder = transactionOrderMapper.selectByOrderNo(orderNo);
        if (transactionOrder == null) {
            throw new RuntimeException("订单不存在");
        }
        transactionOrder.setStatus(status);
        if (paymentStatus != null) {
            transactionOrder.setPaymentStatus(paymentStatus); // 更新付款状态
        }
        transactionOrder.setUpdateTime(new Date());
        transactionOrderMapper.updateById(transactionOrder);
    }

    @Override
    public TransactionOrder getOrderByOrderNo(String orderNo) {
        return transactionOrderMapper.selectOne(new LambdaQueryWrapper<TransactionOrder>()
                .eq(TransactionOrder::getOrderNo, orderNo));
    }

    @Override
    public void updateOrder(TransactionOrder order) {
        transactionOrderMapper.updateById(order);
    }

    @Override
    public List<Map<String, Object>> getPendingPaymentOrders(Long customerId) {
        return transactionOrderMapper.getPendingPaymentOrders(customerId);

    }

    @Override
    public List<Map<String, Object>> getCompletedOrders(Long customerId) {
        return transactionOrderMapper.getCompletedOrders(customerId);
    }

    @Override
    public Map<String, Object> getOrderDetails(String orderNumber) {
        return transactionOrderMapper.getOrderDetails(orderNumber);
    }

    @Override
    public boolean refundOrder(String orderNumber) {
        return transactionOrderMapper.updateRefundStatus(orderNumber, 1) > 0;
    }

}
